<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogVisible"
    width="650px"
  >
    <el-form
      :inline="true"
      :model="formData"
      class="demo-form-inline"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="密码">
        <div class="border_box">
          <!-- <span class="svg-container">
            <svg-icon
              :size="16"
              icon-class="password"
            />
          </span> -->
          <el-input
            v-model="formData.password"
            placeholder="请确认新密码"
            show-password
            autocomplete="new-password"
          />
        </div>
      </el-form-item>
      <el-form-item label="确认密码">
        <div class="border_box">
          <!-- <span class="svg-container">
            <svg-icon
              :size="16"
              icon-class="password"
            />
          </span> -->
          <el-input
            v-model="formData.confirmPwd"
            placeholder="请确认新密码"
            show-password
            autocomplete="new-password"
          />
        </div>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { changePromoterPW } from '@/api/promoter'
export default {
  props: {
    dialogTitle: {
      type: String
    },
    selectRow: {
      type: Object
    }
  },
  data() {
    var validateConfirm = (rule, value, callback) => {
      if (value !== this.form.pwd) {
        callback(new Error('两次输入密码不同！'))
      } else {
        callback()
      }
    }
    var validatePwd = (rule, value, callback) => {
      const reg = /^[^\u4e00-\u9fa5]{0,}$/
      if (value.length < 6) {
        callback(new Error('密码必须大于等于6个字符'))
      } else if (!reg.test(value)) {
        callback(new Error('密码不能包含汉字！'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      formData: {
        id: '',
        password: '',
        confirmPwd: ''
      },
      rules: {
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePwd, trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.getData()
      }
    }
  },
  methods: {
    getData() {
      this.formData.id = this.selectRow.id
    },
    submit() {
      changePromoterPW(this.formData).then(res => {
        this.dialogVisible = false
        this.$emit('getList')
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      })
    },
    changeNumberInput(key, value) {
      const pattern = /^[1-9][0-9]*$/ // 正整数的正则表达式
      // 不符合正整数时
      if (!pattern.test(value)) {
        // input 框绑定的内容为空
        this.formData[key] = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.form_info {
  padding: 20px 30px;
  background: #f5f5f5;
  /deep/.el-divider--horizontal {
    margin: 10px 0;
  }
}
.suffix {
  width: 28px;
  height: 28px;
  background: #e6e6e6;
  border-radius: 0px 3px 3px 0px;
  box-sizing: border-box;
  line-height: 28px;
  text-align: center;
}
.check_form {
  /deep/.el-checkbox__inner {
    border-color: #ff4146;
  }
}
.form_item {
  display: flex;
  line-height: 35px;
  & > label {
    width: 100px;
    color: #666666;
  }
  /deep/.el-input__inner {
    height: 28px;
    border-radius: 3px 0px 0px 3px;
  }
  /deep/.el-input__icon {
    line-height: 28px;
  }
}
</style>
